<template>
  <div class="goods-comment-image">
    <!-- 图片列表 -->
    <div class="list">
      <a
        href="javascript:;"
        :class="{ active: curIndex === index }"
        @click="toggle(index)"
        v-for="(url,index) in pictures"
        :key="url"
      >
        <img :src="url" alt />
      </a>
    </div>
    <!-- 大图预览盒子 -->
    <div class="preview" v-if="isShow">
      <img :src="pictures[curIndex]" alt />
      <i class="iconfont icon-close-new" @click="reset"></i>
    </div>
  </div>
</template>
<script>
/**
 * 1. 点击小图 切换大图
 *    思路：点击小图时拿到当前下标  去数组中通过下标取到要渲染的大图 preview
 *
 * 2. 一开始不显示大图 点击某个小图之后会显示对应大图 并且点击叉号可以关闭大图
 *    思路：v-if
 *
 */
import { ref } from 'vue'
export default {
  name: 'GoodsCommentImage',
  props: {
    pictures: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    // 当前显示图片url
    const curIndex = ref(null)
    const isShow = ref(false)
    function toggle (index) {
      curIndex.value = index
      isShow.value = true
    }
    function reset () {
      isShow.value = false
      curIndex.value = null
    }
    return { curIndex, toggle, isShow, reset }
  }
}
</script>
<style scoped lang="less">
.goods-comment-image {
  .list {
    display: flex;
    flex-wrap: wrap;
    margin-top: 10px;
    a {
      width: 120px;
      height: 120px;
      border: 1px solid #e4e4e4;
      margin-right: 20px;
      margin-bottom: 10px;
      img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
      &.active {
        border-color: @xtxColor;
      }
    }
  }
}
.preview {
  width: 480px;
  height: 480px;
  border: 1px solid #e4e4e4;
  background: #f8f8f8;
  margin-bottom: 20px;
  position: relative;
  img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
  .icon-close-new {
    position: absolute;
    right: 0;
    top: 0;
    width: 30px;
    height: 30px;
    background: rgba(0, 0, 0, 0.2);
    color: #fff;
    text-align: center;
    line-height: 30px;
  }
}
</style>
